package config.knife4j;

import io.swagger.v3.oas.models.info.Contact;
import io.swagger.v3.oas.models.info.Info;
import io.swagger.v3.oas.models.info.License;
import org.springdoc.core.models.GroupedOpenApi;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * Knife4j配置类
 */
@Configuration
public class Knife4jConfig {

    /**
     * 创建分组API的公共方法
     *
     * @param groupName 分组名称
     * @return GroupedOpenApi
     */
    private GroupedOpenApi createGroupedApi(String groupName, String basePackage) {
        return GroupedOpenApi.builder()
                .group(groupName)
                .packagesToScan(basePackage)
                .pathsToMatch("/**")
                .addOpenApiCustomizer(openApi -> openApi.info(apiInfo()))
                .build();
    }

    /**
     * 创建权限分组API
     */
    @Bean
    public GroupedOpenApi securityApi() {
        return createGroupedApi(SystemConstant.Knife4j.SECURITY, SystemConstant.Knife4j.SECURITY_PATH);
    }

    /**
     * 创建系统分组API
     */
    @Bean
    public GroupedOpenApi systemApi() {
        return createGroupedApi(SystemConstant.Knife4j.SYSTEM, SystemConstant.Knife4j.SYSTEM_PATH);
    }

    /**
     * 创建API
     *
     * @param groupName   分组名称
     * @param basePackage 包路径
     * @return GroupedOpenApi
     */
    public GroupedOpenApi createRestApi(String groupName, String basePackage) {
        return createGroupedApi(groupName, basePackage);
    }

    /**
     * API简介信息
     *
     * @return Info
     */
    private Info apiInfo() {
        return new Info()
                .title("Patrick后台管理系统服务接口")
                .description("Patrick后台管理系统服务接口文档...")
                .version("1.0.0")
                .termsOfService("http://doc.xiaominfo.com")
                .contact(new Contact().name("Patrick").url("https://github.com/Patrick-Luo-THR").email("patrick.luo@163.com"))
                .license(new License().name("Apache 2.0").url("http://www.apache.org/licenses/LICENSE-2.0.html"));
    }
}
